import * as echarts from '../../third-utils/ec-canvas/echarts';

const app = getApp();

function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    title: {
      text: '',
      left: 'center'
    },
    // legend: {
    //   data: ['A', 'B', 'C'],
    //   top: 50,
    //   left: 'center',
    //   backgroundColor: 'red',
    //   z: 100
    // },
    grid: {
      containLabel: true
    },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月'],
      // show: false
    },
    yAxis: {
      x: 'center',
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
      // show: false
    },
    series: [{
      name: 'A',
      type: 'line',
      smooth: true,
      data: [18, 36, 65, 30, 78, 40, 33]
    }, {
      name: 'B',
      type: 'line',
      smooth: true,
      data: [12, 50, 51, 35, 70, 30, 20]
    }, {
      name: 'C',
      type: 'line',
      smooth: true,
      data: [10, 30, 31, 50, 40, 20, 10]
    }]
  };

  chart.setOption(option);
  return chart;
}

Page({
    /**
     * 页面的初始数据
     */
    data: {
        ec: {onInit: initChart},
        babyId:'',
        babyInfo:'',
        curCate:1,
        categroyList:['记录列表','身高曲线','体重曲线',],
        infoList:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(e) {
      this.setData({
        babyId:e.babyId
      })
    },
    onShow(){
      this.initData()

    },
    toDel(e){
      let idx=e.currentTarget.dataset.index

      let that=this
    wx.showModal({
      title: '提示',
      content: '确定删除该记录吗？',
      success (res) {
        if (res.confirm) {
          wx.request({
            url: getApp().globalData.apiBaseUrl + 'babyBMI/delete',
            data: {
              id: that.data.infoList[idx].id
            },
            method: 'post',
            success: (res) => {
              if (res.data.code == 200) {
                that.data.infoList.splice(idx,1)
                that.setData({
                  infoList:that.data.infoList
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    },
    toAdd(){
      wx.navigateTo({
        url: '/pages/bodyAdd/index?babyId='+this.data.babyId,
      })
    },
    toEdit(e){
      wx.navigateTo({
        url: '/pages/bodyAdd/index?babyId='+this.data.babyId+'&id='+e.currentTarget.dataset.id,
      })
    },
   
    initData(){
        
      wx.request({
        url: 'https://babyplanet.isv-dev.youzan.com/api/babyBMI/findList',
        data: {
          babyId:this.data.babyId
        },
        method: 'post',
        success: (res) => {
          if (res.data.code == 200) {
            this.setData({
              infoList:res.data.data
            })
          }
        }
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})